#include "ArrayGenome.h"
#include "SimpleGA.h"

#define GA_POP_SIZE 2000
#define GA_NUM_GEN  100
#define GA_PMUT     0.2
#define GA_PCROSS   0.4

void evolve(int argc, char**argv) 
  {
	long seed = 9999; 

	#ifdef USE_MPI
	int procid;
	MPI_Init (& argc ,& argv );
	MPI_Comm_rank(MPI_COMM_WORLD,&procid);
	#endif

	int popsize  = GA_POP_SIZE;
	int ngen     = GA_NUM_GEN;
	float pmut   = GA_PMUT;
	float pcross = GA_PCROSS;

	GARandom *randObj = new GARandom(seed);
    	ArrayGenome genome(10); 
	genome.setRandomObj(randObj);
	SimpleGA ga(&genome, popsize, pcross, pmut, randObj);

	GASelectionScheme *selector = new GATournamentSelector();
        ga.selector(selector);

	const GAPopulation& initpop = ga.population();
	ArrayGenome& initbest = (ArrayGenome&) initpop.best();

	for (int g=0; g<ngen; g++)
	  {
	    ga.step();
	    const GAPopulation& pop = ga.population();
	    float avg = pop.fitave();

	    ArrayGenome& best = (ArrayGenome&) pop.best();
	    cout << ">>>>>>>  Gen " << g << ", Max: " << pop.fitmax() 
		 << " (id = " << best.getId() 
		 << "), Avg: " << avg << endl;
	  }

#ifdef USE_MPI
	MPI_Finalize ();
#endif
  }

int main(int argc, char**argv) 
	{
	evolve(argc, argv);
	}

